package cy;

import cn.hutool.json.JSONUtil;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

public class JSONTest01 {
    @Test
    public void test01(){
        student student = new student(20131210,"无忌","男",25);
        String jsonStr = JSONUtil.toJsonStr(student);
        System.out.println(jsonStr);
    }
    @Test
    public void test02(){
        String jsonStr="{\"sno\":20131210,\"sname\":\"无忌\",\"gender\":\"男\",\"age\":25}";
        student student = JSONUtil.toBean(jsonStr, student.class);
        System.out.println(student.getGender());
    }
    @Test
    public void test03(){
        List<student> students = Arrays.asList(
                new student(20131210, "无忌", "男", 25),
                new student(20131217, "敏敏", "女", 20)
        );
        String jsonStr = JSONUtil.toJsonStr(students);
        System.out.println(jsonStr);
        System.out.println(JSONUtil.formatJsonStr(jsonStr));
    }
    @Test
    public void test04(){
        String jsonStr="[{\"sno\":20131210,\"sname\":\"无忌\",\"gender\":\"男\",\"age\":25},{\"sno\":20131210,\"sname\":\"敏敏\",\"gender\":\"女\",\"age\":20}]";
        List<student> students = JSONUtil.parseArray(jsonStr).toList(student.class);
        students.forEach(System.out::println);

    }
    @Test
    public void test05(){
        List<student> students = Arrays.asList(
                new student(20131210, "无忌", "男", 25),
                new student(20131217, "敏敏", "女", 20)
        );
        Map<String, student> map = students.stream().collect(Collectors.toMap(student -> student.getSno() + " ", student -> student));
        System.out.println(JSONUtil.toJsonStr(map));
    }
}
